// pages/salesman/salesman.js
var app = getApp();
const stateOptions = [{
  name: "全部",
  value: ''
}, {
  name: "正常",
  value: 1
}, {
  name: "注销",
  value: 2
}];
const pageSize = 20;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [
      {
        id: 1,
        icon: '/images/icon_franchiseeManagement.png',
        name: '销售管理',
        url: '../salesmans/salesmanList/salesmanList',
        status: true
      },
      {
        id: 2,
        icon: '/images/icon_cardManagement.png',
        name: '水卡管理', 
        url: '../salesmans/salesmanCard/salesmanCard',
        status: true
      },
      {
        id: 3,
        icon: '/images/icon_performanceManagement.png',
        name: '业绩管理',
        url: '../salesmans/salesmanPerformance/salesmanPerformance',
        status: true
      },
      {
        id: 4,
        icon: '/images/icon_accountManagement.png',
        name: '账户管理',
        url: '../salesmans/salesmanAccount/salesmanAccount',
        status: true
      }
    ],
    count_data:[],
    page: 1, //当前页数
    tplList: [],
    loadmore: false,
    nomore: false,
    isFilter: false,
    drawer: false,
    conditions: {
      stateIndex: "",
      full_name: "",
      tel: "",
    },
    stateOptions,
    empty: false,
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.statistical();
    this.salesmanList();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.salesmanList();
    }
  },

  statistical(){
    var that = this;
    app.ajax({
      url: 'Partner/Salesman/statistical',
      method: "POST",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            count_data: res.data.data
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },

  reloadData() {
    this.setData({
      page: 1,
      tplList: [],
      loadmore: false,
      nomore: false,
    })
    wx.showLoading();
    this.salesmanList();
  },


  formatIdCard(idCard) {
    return idCard.replace(/^(.{6})(?:\d+)(.{4})$/, "$1****$2");
  },




  //业务员列表
  salesmanList() {
    app.ajax({
      url: 'Partner/Salesman/salesmanList',
      data: {
        page: this.data.page,
        row: pageSize,
        full_name: this.data.conditions.full_name,
        tel: this.data.conditions.tel,
        state: this.data.conditions.stateIndex !== '' ? this.data.stateOptions[this.data.conditions.stateIndex].value : ''
      },
      success: (res) => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code > 0) {
          const source = res.data.data;
          source.data.forEach(current => {
            return current.id_card = this.formatIdCard(current.id_card)
          })
          let list = [];
          if (this.data.page==1){
            list = source.data;
          } else {
            list = this.data.tplList.concat(source.data);
          }
          this.setData({
            tplList: list,
            empty: source.page.total == 0 ? true : false,
            loadmore: source.page.total > list.length ? true : false,
            nomore: source.page.total <= list.length ? true : false,
            drawer: false
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },
  //添加业务员
  addFranchiseee() {
    wx.navigateTo({
      url: '../salesmans/salesmanListEdit/salesmanListEdit?type=1',
    })
  },
  //编辑业务员
  editFranchiseee(e) {
    wx.navigateTo({
      url: '../salesmans/salesmanListEdit/salesmanListEdit?type=2&s_id=' + e.currentTarget.dataset.s_id,
    })
  },
  //查看业务员
  lookFranchiseee(e) {
    wx.navigateTo({
      url: '../salesmans/salesmanListEdit/salesmanListEdit?type=3&s_id=' + e.currentTarget.dataset.s_id,
    })
  },
  //删除业务员
  deleteFranchiseee(e) {
    var that = this;
    wx.showModal({
      content: '是否删除销售?',
      confirmText: '是',
      cancelText: '否',
      success: function (res) {
        if (res.confirm) {
          app.ajax({
            url: 'Partner/Salesman/deleteSalesman',
            method: "POST",
            data: {
              id: e.currentTarget.dataset.s_id
            },
            success: function (res) {
              if (res.data.code == 1000) {
                app.showToast(res.data.message, "success");
                setTimeout(() => {
                  that.reloadData();
                }, 1500)
              } else {
                app.showToast(res.data.message);
                return false;
              }
            }
          })
        }
      }
    })
  },

  //跳转提现账户余额详情
  presentBalance(e) {
    wx.navigateTo({
      url: '../salesmans/presentBalance/presentBalance?id=' + e.currentTarget.dataset.s_id,
    })
  },
  //跳转充值卡账户余额详情
  rechargeBalance(e) {
    wx.navigateTo({
      url: '../salesmans/rechargeBalance/rechargeBalance?id=' + e.currentTarget.dataset.s_id,
    })
  },
  //跳转水卡管理
  navCardList(e){
    wx.navigateTo({
      url: '../salesmans/fictitiousCard/fictitiousCard?id=' + e.currentTarget.dataset.s_id,
    })
  },
  //跳转业绩管理
  salesmanPerformance(e){
    wx.navigateTo({
      url: '../salesmans/salesmanPerformance/salesmanPerformance?id=' + e.currentTarget.dataset.s_id,
    })
  },

  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },
  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },

  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },
  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete (this.condition)
  },
  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },
  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete (this.condition);
    this.reloadData();
  },
})